﻿
namespace Com.Christoc.HomeController.Admin
{
    using System;
    using System.Web.UI.WebControls;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Localization;

    public partial class DeviceList : HomeControllerModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: add in a confirmation window when clicking on delete in the GridView Rows
            if (!IsPostBack)
                Localization.LocalizeGridView(ref DevicesGridView, LocalResourceFile);
            if (!IsEditable)
            {
                AddDeviceLinkButton.Visible = false;
            }
        }

        protected void AddDeviceLinkButton_Click(object sender, EventArgs e)
        {
            DevicesFormView.Visible = true;
            DevicesGridView.DataBind();

            var ddl = (DropDownList)DevicesFormView.FindControl("DeviceTypeList");
            ddl.DataSource = DeviceTypesListDataSource;
            ddl.DataBind();

            var ddlr = (DropDownList)DevicesFormView.FindControl("RoomsList");
            ddlr.DataSource = RoomListDataSource;
            ddlr.DataBind();
        }

        protected void DevicesLinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            var ddlr = (DropDownList)DevicesFormView.FindControl("RoomsList");
            var ddl = (DropDownList)DevicesFormView.FindControl("DeviceTypeList");

            //if we need to update the userid and moduleid we can here
            var dvl = (HomeController_Device)e.NewObject;
            dvl.CreatedByUserId = UserId;
            dvl.LastUpdatedByUserId = UserId;
            dvl.LastUpdatedByUserId = UserInfo.UserID;
            dvl.LastUpdated = dvl.DateCreated = DateTime.Now.ToUniversalTime();

            var hdr = new HomeController_DeviceRoom { roomid = Convert.ToInt32(ddlr.SelectedValue) };
            dvl.HomeController_DeviceRooms.Insert(0, hdr);

            var dtm = new HomeController_DeviceTypeMap { devicetypesid = Convert.ToInt32(ddl.SelectedValue) };
            dvl.HomeController_DeviceTypeMaps.Insert(0, dtm);

        }

        protected void DevicesLinqDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            DevicesGridView.DataBind();
        }

        protected void DevicesLinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
        {
            var dt = (HomeController_Device)e.NewObject;
            dt.LastUpdatedByUserId = UserInfo.UserID;
            dt.LastUpdated = DateTime.Now.ToUniversalTime();
            //dt.HomeController_DeviceRooms



            ////todo: update device type and room
            //var hdr = new HomeController_DeviceRoom { roomid = Convert.ToInt32(ddlr.SelectedValue) };
            //dvl.HomeController_DeviceRooms.Insert(0, hdr);

            //var dtm = new HomeController_DeviceTypeMap { devicetypesid = Convert.ToInt32(ddl.SelectedValue) };
            //dvl.HomeController_DeviceTypeMaps.Insert(0, dtm);


        }

        protected void DevicesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                var dt = ((HomeController_Device)(e.Row.DataItem));

                if ((PortalSecurity.IsInRole("Administrators"))
                || (UserController.GetCurrentUserInfo().UserID == dt.CreatedByUserId))
                {
                    e.Row.Cells[0].Enabled = true;
                }
                else
                {
                    e.Row.Cells[0].Text = string.Empty;
                }

                //DeviceTypesListDataSource
                var ddl = (DropDownList)e.Row.Cells[6].FindControl("DeviceTypeList");
                ddl.DataSource = DeviceTypesListDataSource;
                ddl.DataBind();

                var ddlr = (DropDownList)e.Row.Cells[5].FindControl("RoomList");
                ddlr.DataSource = RoomListDataSource;
                ddlr.DataBind();
            }
        }


        protected void DevicesGridView_RowUpdating(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                var ddl = (DropDownList)e.Row.Cells[6].FindControl("DeviceTypeList");
                
            }
        }

        protected void InsertButton_Click(object sender, EventArgs e)
        {
            DevicesFormView.Visible = false;
            AddDeviceLinkButton.Text = Localization.GetString("UpdateSuccessful", LocalResourceFile);//"Update Successful - Add Another Listing";
            DevicesGridView.DataBind();
        }

        protected void InsertCancelButton_Click(object sender, EventArgs e)
        {
            DevicesFormView.Visible = false;
            DevicesGridView.DataBind();
        }
    }
}
